home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_02
/
pugh
/
sort.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1994-12-11
|
1KB
|
72 lines
#include <stdlib.h>
#include <iostream.h>
class A
{
private:
struct DataType
{
double x,y;
static int compare_function(const void * a, const void * b)
{
const DataType *first = (DataType*) a;
const DataType *second = (DataType*) b;
if (first->x > second->x)
return 1;
else if (first->x < second->x)
return -1;
else
return 0;
}
} * data;
int size;
public:
A(int n, double * x, double * y);
~A();
void sort();
void list();
};
A::A(int n, double * x, double * y)
{
size = n;
data = new DataType[size];
for (int i = 0; i < size; i++)
{
data[i].x = x[i];
data[i].y = y[i];
}
};
A::~A()
{
delete [] data;
}
void A::sort()
{
qsort (data, size, sizeof(DataType), DataType::compare_function);
}
void A::list()
{
for (int i = 0; i < size; i++)
{
cout << data[i].x << " " << data[i].y << endl;
}
}
main()
{
double xarray[5] = {4.0, 2.0, 3.0, 5.0, 1.0};
double yarray[5] = {0.1, 0.2, 0.3, 0.4, 0.5};
A AInst (3, xarray, yarray);
cout << "Original list" << endl;
AInst.list();
AInst.sort();
cout << endl << "Sorted list" << endl;
AInst.list();
return 0;
}